﻿using System;
using System.Linq;
using System.Web.Mvc;
using Lbb.Localization;
using Lbb.Web.Mvc;
using Lbb.URating.Controllers;
using Lbb.URating.DL;
using Lbb.URating.Localization;
using Lbb.URating.Models;

namespace Lbb.URating.Areas.admin.Controllers
{
    /// <summary>
    /// 评分分类控制器
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickController : URatingControllerBase
    {
        #region Fields
        private const string _preKey = "U_Click_";
        private readonly static string[] _listKeys = URatingControllerBase.listKeys.Union(new[] { 
            "isRating", "minRating" ,"maxRating","categoryIds"
        }).ToArray();
        #endregion Fields

        #region Pages
        /// <summary>
        /// 列表页面
        /// </summary>
        public ActionResult Index(int? pageNumber, int? pageSize, string keyword, string orderby, bool? isRating, decimal? minRating, decimal? maxRating, string categoryIds)
        {
            Response.Expires = 0;
            CacheArgs(_preKey, _listKeys);
            var model = new U_ClickDataList();
            model.FillData(U_ClickRepository.Find(model.Keyword, model.IsRating, model.MinRating, model.MaxRating, model.CategoryIds));
            return View(IsRequestPartial ? "List" : "Index", model);
        }
        /// <summary>
        /// 返回到上次查看的列表页
        /// </summary>
        public ActionResult RedirectToIndex()
        {
            return RedirectToAction("Index", GetCacheArgs(_preKey, _listKeys));
        }
        /// <summary>
        /// 添加/编辑页面
        /// </summary>
        public ActionResult Edit(Guid? id, Guid? categoryID)
        {
            var model = U_ClickRepository.Get(id) ?? new U_Click
            {
                CategoryID = categoryID ?? Guid.Empty,
                MinRating = 0,
                MaxRating = 10,
                IncrementValue = 1
            };
            return View(model);
        }
        /// <summary>
        /// 保存评分分类
        /// </summary>
        [HttpPost]
        public ActionResult Edit(U_Click model)
        {
            try
            {
                if (U_ClickCategoryRepository.Get(model.CategoryID) == null) ModelState.AddModelError("CategoryID", string.Format(OperatorRes.NotFindSelectedObj, U_ClickRes.CategoryID));

                if (model.MinRating > model.MaxRating)
                {
                    ModelState.AddModelError("MinRating", U_ClickRes.ErrorMinRatingGreaterThanMaxRating);
                }

                if (ModelState.IsValid)
                {
                    if (model.ClickID == Guid.Empty)
                    {
                        U_ClickRepository.Add(model);
                    }
                    else
                    {
                        U_ClickRepository.Update(model);
                    }
                    U_ClickCategoryRepository.Save();
                    return RedirectToIndex();
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return View(model);
        }
        /// <summary>
        /// 删除评分分类
        /// </summary>
        [HttpPost]
        public ActionResult Delete(Guid[] id)
        {
            var result = new JsonResponse();

            if (IsValidValue(ref id))
            {
                try
                {
                    U_ClickRepository.DeleteRange(U_ClickRepository.Find(d => id.Contains(d.ClickID)));
                    U_ClickRepository.Save();
                }
                catch (Exception ex)
                {
                    result.AppendMessage(ex);
                }
            }
            else
            {
                result.AppendMessage(string.Format(OperatorRes.PleaseSelectObj, OperatorRes.Delete, U_ClickRes.ClassName));
            }

            result.success = string.IsNullOrEmpty(result.message);
            return Json(result);
        }
        /// <summary>
        /// 排序
        /// </summary>
        [HttpPost]
        public ActionResult Ordering(Guid[] id, int[] ordering)
        {
            var result = new JsonResponse();

            if (IsValidValue(ref id))
            {
                try
                {
                    var n = id.Length < ordering.Length ? id.Length : ordering.Length;
                    for (var i = 0; i < n; i++)
                    {
                        var item = U_ClickRepository.Get(id[i]);
                        if (item != null)
                        {
                            item.Ordering = ordering[i];
                        }
                    }
                    U_ClickRepository.Save();
                }
                catch (Exception ex)
                {
                    result.AppendMessage(ex);
                }
            }
            else
            {
                result.AppendMessage(string.Format(OperatorRes.PleaseSelectObj, OperatorRes.Ordering, U_ClickRes.ClassName));
            }

            result.success = string.IsNullOrEmpty(result.message);
            return Json(result);
        }
        #endregion Pages


    }
}
